#pragma once
#include "resourceppc.h"
#include "ListData.h"
#include "Theme.h"
#include "Menubar.h"
#include "pimstore.h"

// Returns number of elements
#define dim(x) (sizeof(x) / sizeof(x[0]))   

//----------------------------------------------------------------------
// Generic defines and data types
//
struct decodeUINT {                             // Structure associates
    UINT Code;                                  // messages 
                                                // with a function. 
    LRESULT (*Fxn)(HWND, UINT, WPARAM, LPARAM);
}; 

#define SZ_APP_NAME                 TEXT("iContact")


//burt: mouse gestures begin
#define MOUSE_GESTURE_MINDELTAX    40 // % of screen width
#define MOUSE_GESTURE_MINDELTAY    40 // % of screen height

#define TRANSITION_REFRESH_RATE    30

enum EScreenMode
{
  smList      = 0,
  smDetails   = 1
};

enum ETransitionType
{
  ttSlideLeft,
  ttSlideRight,
  ttShowPopup,
  ttHidePopup,
  ttShowSkk,
  ttHideSkk
};

enum EPopupWindows
{
  pwNone       = 0,
  pwKeyboard   = 1,
  pwCategories = 2
};

enum EMouseGesture
{
  mgNone    = 0,
  mgRtoL    = 1,  // <
  mgLtoR    = 2,  // >
  mgUtoD    = 3,  // v
  mgDtoU    = 4,  // ^
  mgURtoDL  = 5,  // ./
  mgDLtoUR  = 6,  //    /'
  mgULtoDR  = 7,  // up-left to down-right
  mgDRtoUL  = 8    // down-right to up-left
};
//burt: mouse gestures end

enum EMenuType
{
  mtList        = 0,
  mtDetails     = 1,
  mtCallDetails = 2
};

enum EMouseArea
{
  maNone        = 0,
  maList,
  maDetails,
  maTitlebar,
  maMenubar,
  maPopup,
  maSearchKbd
};

//----------------------------------------------------------------------
// Function prototypes
//
bool FindVersion();
HWND InitInstance(HINSTANCE, LPWSTR, int);
int  TermInstance(HINSTANCE, int);

void RegisterCallNotifications(HWND hWnd);
void UnregisterCallNotifications();
void ApplySettings();
void ApplyCommandLine(LPWSTR cmdLine);
void ApplyIDialerCommand(HWND hWnd, LPWSTR cmdLine);
TCHAR* GetCommandLineOption(TCHAR* optionPtr, TCHAR* &name, TCHAR* &value);

// Window procedures
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);

// Message handlers
LRESULT DoPaintMain(HWND, UINT, WPARAM, LPARAM);
LRESULT DoActivate(HWND, UINT, WPARAM, LPARAM);
void    DoActivated(HWND);
void    DoDeactivated(HWND);
LRESULT DoSize(HWND, UINT, WPARAM, LPARAM);
LRESULT DoLButtonDown(HWND, UINT, WPARAM, LPARAM);
LRESULT DoMouseMove(HWND, UINT, WPARAM, LPARAM);
LRESULT DoLButtonUp(HWND, UINT, WPARAM, LPARAM);
LRESULT DoTimer(HWND, UINT, WPARAM, LPARAM);
LRESULT DoKeyDown(HWND, UINT, WPARAM, LPARAM);
LRESULT DoKeyUp(HWND, UINT, WPARAM, LPARAM);
bool    DoKeyHook(HWND hWnd, DWORD nKey, DWORD nKeyData);
LRESULT DoChar(HWND, UINT, WPARAM, LPARAM);
LRESULT DoCommand(HWND, UINT, WPARAM, LPARAM);
LRESULT DoCtlColorEdit(HWND, UINT, WPARAM, LPARAM);
LRESULT DoDestroyMain(HWND, UINT, WPARAM, LPARAM);

LRESULT DoTitlebarCallback (HWND, UINT, WPARAM, LPARAM);
LRESULT DoDbNotificationCallback (HWND, UINT, WPARAM, LPARAM);
LRESULT DoPoomContactCreated(HWND, UINT, WPARAM, LPARAM);
LRESULT DoPoomContactChanged(HWND, UINT, WPARAM, LPARAM);
LRESULT DoPoomContactDeleted(HWND, UINT, WPARAM, LPARAM);
LRESULT DoSimContactRefresh(HWND, UINT, WPARAM, LPARAM);
LRESULT DoPoomPictureLoaded(HWND, UINT, WPARAM, LPARAM);

// Search Keyboard
LRESULT DoSkkStringUpdated(HWND, UINT, WPARAM, LPARAM);
LRESULT DoSkkModeChanged(HWND, UINT, WPARAM, LPARAM);
LRESULT DoSkkClose(HWND, UINT, WPARAM, LPARAM);

// Info
LRESULT DoInfoGetCurrentTab(HWND, UINT, WPARAM, LPARAM);
LRESULT DoInfoGetCallType(HWND, UINT, WPARAM, LPARAM);
LRESULT DoInfoGetContactsList(HWND, UINT, WPARAM, LPARAM);

void InitSurface(HWND);

//
// Drawing functions
//
void DrawTransition(HDC hdc, ETransitionType tt);
void DrawTransitionSlide(HDC hdc, ETransitionType tt);
void DrawTransitionPopup(HDC hdc, ETransitionType tt);
void DrawTransitionSkk(HDC hdc, ETransitionType tt);

void DrawScreenOn(HDC, EScreenMode, HDC, const RECT&);
void DrawKeyboardOn(HDC, const RECT&);

//
// Helper functions
//

void CalculateRects();
void UpdateMenuRect();
void CalculateKeyboard();

void SetMenu(HWND hWnd, Menu* pMenu, bool redraw);

bool PopulateDetailsInfo(HWND, int item);
void ReloadAfterDetails(HWND);
void ExpandDetails(HWND);

void StartTransition(HWND hWnd, ETransitionType tr, int duration);
void TransitionFinished(HWND hWnd);

void SwitchTab(HWND, EMainTabs);
void ReloadList(HWND hWnd, EMainTabs forTab, bool fullReload = false);

void DoDBNotificationTimer();

// Mouse Actions
void DoPopupLButtonDown(HWND hWnd, int x, int y);
void DoPopupMouseMove(HWND hWnd, int x, int y);
void DoPopupLButtonUp(HWND hWnd, int x, int y);

void DoMinimizeClick(HWND hWnd);
void DoTitlebarClick(HWND hWnd, int x, int y);

// search box
void OnSearchBoxChange(HWND hWnd);

// list
void OnCmdListFavorites(HWND hWnd, int nCmdFlags);
void OnCmdListCallLog(HWND hWnd, int nCmdFlags);
void OnCmdListContacts(HWND hWnd, int nCmdFlags);
void OnCmdListDialer(HWND hWnd, int nCmdFlags);
void OnCmdListKeyboard(HWND hWnd, int nCmdFlags);
// details
void OnCmdDetailsBack(HWND hWnd, int nCmdFlags);
void OnCmdDetailsView(HWND hWnd, int nCmdFlags);
void OnCmdDetailsEdit(HWND hWnd, int nCmdFlags);
void OnCmdDetailsDelete(HWND hWnd, int nCmdFlags);
void OnCmdDetailsFavorite(HWND hWnd, int nCmdFlags);
// call log
void OnCmdDetailsClNewContact(HWND hWnd, int nCmdFlags);
void OnCmdDetailsClGoToContact(HWND hWnd, int nCmdFlags);
void OnCmdDetailsClDeleteCall(HWND hWnd, int nCmdFlags);
// sim
void OnCmdDetailsSimNewContact(HWND hWnd, int nCmdFlags);
void OnCmdDetailsSimGoToOutlook(HWND hWnd, int nCmdFlags);
// set picture
void OnCmdDetailsSetPicture(HWND hWnd, int nCmdFlags);
// set ringtone
void OnCmdDetailsSetRingtone(HWND hWnd, int nCmdFlags);
// list commands
void OnCmdListShowDetails(HWND hWnd, int nCmdFlags);
void OnCmdListSetCategory(HWND hWnd, int nCmdFlags);
void OnCmdListHideCategories(HWND hWnd, int nCmdFlags);

bool DoMouseGesture(HWND hWnd, int dx, int dy);
EMouseGesture ParseMouseGesture(int dx, int dy);

void RollCallHistory(HWND hWnd, bool forward);

bool DoTabAction(HWND hWnd);
bool DoDefaultAction(HWND hWnd, int item);

void ToggleMenu(HWND hWnd, EMenuType type);
void ToggleFavorite(HWND hWnd, int item);

void ClearFilter(HWND hWnd, bool redraw);
void ToggleSearchKeyboard(HWND hWnd, bool bShow);

enum EBgTasks
{
  bgtNone       = 0x00,
  bgtPictures   = 0x01,
  bgtSim        = 0x02,
  bgtPoom       = 0x04
};
int GetBackgroundTasks();
LRESULT DoThreadBusy(HWND, UINT, WPARAM, LPARAM);
LRESULT DoBusyMode(HWND, UINT, WPARAM, LPARAM);
LRESULT DoBusyProgress(HWND, UINT, WPARAM, LPARAM);
//RINGTONE
bool GetContactRingtone(IItem *pItem, LPTSTR pszRingtone, DWORD dwBufferSize);

//Helper function for getting config path
bool GetConfigExe(wchar_t *path);
